package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.Search.SearchIndexVo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.service.SearchService;
import org.apache.catalina.UserDatabase;
import org.apache.catalina.security.SecurityUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("wx/search")
public class SearchController {


    @Autowired
    SearchService searchService;

    @RequestMapping("index")
    public BaseRespVo searchIndex() {
        SearchIndexVo searchIndexVo = searchService.showIndex();
        BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
        baseRespVo.setErrno(0);
        baseRespVo.setErrmsg("成功");
        baseRespVo.setData(searchIndexVo);
        return baseRespVo;
    }

    @RequestMapping("helper")
    public BaseRespVo searchHelper(String keyword){
        searchService.insertKeyword(keyword);
        BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
        baseRespVo.setErrno(0);
        baseRespVo.setErrmsg("成功");
        baseRespVo.setData(keyword);
        return baseRespVo;

    }

    @RequestMapping("clearhistory")
    public BaseRespVo cleanHistory(){
        //拿用户ID
        Subject subject = SecurityUtils.getSubject();
        MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer uid = primaryPrincipal.getId();

        searchService.cleanHistory(uid);
        BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        return baseRespVo;
    }
}
